<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use app\index\model\Secondhandle as Second;
use think\Db;

class Secondhandle extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}


	/**
	*二手车首页品牌显示
	**/

	public function brand()
	{
		//获取品牌数组
		$brands = Db('used_classify')->where('pid',2)->select();		
		if($brands){
            $this->apiSuccess($brands);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*二手车搜索
	*@param  int page 		  分页的第几页
	*@param  int pagelimite   每页显示几条
	**/
	public function search()
	{
		//获取表单提交的查询title
		// $title = $_GET['title'];
		$title = 1;
		//接收数据
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		//模糊查询条件
		$title_like['title']=['like',"%{$title}%"];
		//置顶,审批,未交易,查询
		$topList = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where($title_like)->page($page,$pagelimit)->select();
		//未置顶,审批,未交易,查询
		$noTopList = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where($title_like)->page($page,$pagelimit)->select();
		//审批,未交易,查询
		$List = array_merge($topList,$noTopList);
		// dump($List);
		//将数组中的图集,标题,表显里程,全款价格,车辆所在地,上牌时间,交强险过期时间,卖家附言进行处理
		$this->dohtml($List,$page,$pagelimit);
	}

	/**
	*二手车车型,品牌
	**/
	public function type()
	{
		// $this->usedclassify(1);
		$classifyInfo = Db('used_classify')->select();
		$classify = Db('used_classify')->where('pid',1)->select();
		$i = 0;
		//遍历车型数组		
		for(;$i<count($classify);$i++){
			$typelist[$i]['type'][] = $classify[$i]['name'];
			$typelist[$i]['type'][] = $classify[$i]['id'];
			$j = 0;
			//遍历所有分类数组,查看数组中不同车型下对应的品牌
			for (; $j < count($classifyInfo); $j++) { 
				$arr = explode(',',$classifyInfo[$j]['pid']);
				if($arr[0] == $classify[$i]['id'] && $arr[1] == 0){
					$typelist[$i]['type']['brand'][] = $classifyInfo[$j]['name'];
					$typelist[$i]['type']['brand'][] = $classifyInfo[$j]['id'];
				}
			}
		}
		dump($typelist);
		if($typelist){
            $this->apiSuccess($typelist);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*二手车最新上架
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function latest()
	{
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doShow('create_time','desc',$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}

	/**
	*二手车推荐二手车
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function recommend()
	{
		//分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doIfShow('is_recommend',1,$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}

	/**
	*二手车默认排序
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function moRen()
	{
		// //分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doShow('id','desc',$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}


	/**
	*二手车价格最低(按价格排序)
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function lowPrice()
	{
		// //分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doShow('price','asc',$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}

	/**
	*二手车价格最高(按价格排序)
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function highPrice()
	{
		// //分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doShow('price','desc',$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}

	/**
	*二手车上牌时间倒序
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function applyTime()
	{
		//分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doShow('apply_time','desc',$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}

	/**
	*二手车降价急售
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function urgentSale()
	{
		//分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		//获取数据
		$list = $this->doIfShow('low_price',1,$page,$pagelimit,$type,$brand,$low,$high);
		$this->dohtml($list,$page,$pagelimit);
	}

	/**
	*公用方法:order 排序
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function doShow($field,$way,$page,$pagelimit,$type,$brand,$low,$high)
	{
		if($type > 0 && $brand > 0){
			//二手车的状态 审核,置顶,未交易
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('type',$type)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('type',$type)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}elseif($type > 0 && $brand = 0){
			//二手车的状态 审核,置顶,未交易
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('type',$type)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('type',$type)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}elseif($type = 0 && $brand > 0){
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}else{
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('price','>=',$low)->where('price','<',$high)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}
		
	}


	/**
	*公用方法:where 判断
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function doIfShow($field,$value,$page,$pagelimit,$type,$brand,$low,$high)
	{
		if($type > 0 && $brand > 0){
			//二手车的状态 审核,置顶,未交易
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('type',$type)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('type',$type)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}elseif($type > 0 && $brand = 0){
			//二手车的状态 审核,置顶,未交易
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('type',$type)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('type',$type)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}elseif($type = 0 && $brand > 0){
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('brand',$brand)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}else{
			$list1 = Db('used_truck')->where('is_top',1)->where('status',1)->where('deal',0)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
			$list2 = Db('used_truck')->where('is_top',0)->where('status',1)->where('deal',0)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
			$list = array_merge($list1,$list2);
			return $list;
		}
	}

	/**
	*公共方法:将数组中的图集,车辆所在地,标题,表显里程,全款价格,上牌时间,交强险过期时间,卖家附言进行处理
	*@param   $arr 		 	 处理的数组
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	**/
	public function dohtml($arr,$page,$pagelimit)
	{
		$i = 0;
		for(;$i<count($arr);$i++){
			$arr[$i]['photo'] = unserialize($arr[$i]['photo']);
			$arr[$i]['title'] = htmlspecialchars_decode($arr[$i]['title']);
			$arr[$i]['mileage'] = htmlspecialchars_decode($arr[$i]['mileage']);
			$arr[$i]['price'] = htmlspecialchars_decode($arr[$i]['price']);
			$arr[$i]['apply_time'] = htmlspecialchars_decode($arr[$i]['apply_time']);
			$arr[$i]['past_time'] = htmlspecialchars_decode($arr[$i]['past_time']);
			$arr[$i]['content'] = htmlspecialchars_decode($arr[$i]['content']);
			$arr1[] = $arr[$i];
		}
		// dump($arr1);
		$num = count($arr);
		if($num){
			$count = count($arr1);
			$pagecount = ceil($count/$pagelimit);
			// $List['pagecount'] = $pagecount;
			$list['group'] = $arr1;
			$list['pagecount'] = $pagecount;
			// dump($list);
			$this->apiSuccess($list);
		}else{
			 $this->apiError('没有查到信息');
		}
		
	}

}